/*
 * RotateDialog.java
 *
 * Created on Oct 9, 2011, 5:58:02 PM
 */
package com.photoexpert;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author PANYIXIN
 */
class RotateDialog extends javax.swing.JDialog {

    /** Creates new form RotateDialog */
    RotateDialog() {
        super(App.Instance(), true);
        initComponents();
        _preview.addMouseListener((RotatePreviewPane)_preview);
        _preview.addMouseMotionListener((RotatePreviewPane)_preview);
        setLocation(200, 200);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _degreeLabel = new javax.swing.JLabel();
        _degree = new javax.swing.JTextField();
        _previewLabel = new javax.swing.JLabel();
        _preview = new RotatePreviewPane();
        _cancel = new javax.swing.JButton();
        _ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Rotate");
        setModal(true);
        setResizable(false);

        _degreeLabel.setText("Degree:");

        _degree.setText("0");
        _degree.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _degreeFocusLost(evt);
            }
        });

        _previewLabel.setText("Preview:");

        _preview.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        _preview.setPreferredSize(new java.awt.Dimension(252, 190));

        javax.swing.GroupLayout _previewLayout = new javax.swing.GroupLayout(_preview);
        _preview.setLayout(_previewLayout);
        _previewLayout.setHorizontalGroup(
            _previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 250, Short.MAX_VALUE)
        );
        _previewLayout.setVerticalGroup(
            _previewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        _cancel.setText("Cancel");
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        _ok.setText("OK");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_preview, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(_ok, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                            .addComponent(_cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_degreeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(_degree, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(_previewLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_ok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_cancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(_degreeLabel)
                            .addComponent(_degree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_previewLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_preview, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event__cancelActionPerformed

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        Implementor.Instance().setImage(_image);
        setVisible(false);
        dispose();
    }//GEN-LAST:event__okActionPerformed

    private void _degreeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__degreeFocusLost
        int degree = Integer.parseInt(_degree.getText());
        if(degree == 0)
            _image = _srcImage;
        else {
            double d = degree / 180d * Math.PI;
            int w = _srcImage.getWidth();
            int h = _srcImage.getHeight();
            int w1 = (int)(w * Math.abs(Math.cos(d)) + h * Math.abs(Math.sin(d)));
            int h1 = (int)(w * Math.abs(Math.sin(d)) + h * Math.abs(Math.cos(d)));
            BufferedImage image = new BufferedImage(w1, h1, _srcImage.getType());
            Graphics g = image.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, w1, h1);
            g.drawImage(_srcImage, (w1 - w) / 2, (h1 - h) / 2, null);
            g.dispose();
            AffineTransform tx = new AffineTransform();
            tx.rotate(d, w1 / 2, h1 / 2);
            AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
            _image = new BufferedImage(w1, h1, _srcImage.getType());
            g = _image.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, w1, h1);
            g.dispose();
            op.filter(image, _image);
        }
        _preview.repaint();
    }//GEN-LAST:event__degreeFocusLost

    class RotatePreviewPane extends JPanel implements MouseListener, MouseMotionListener {

        @Override
        public void paint(Graphics g) {
            g.drawImage(_image, 1, 1, 250, 190, _curX, _curY, _curX + 250, _curY + 190, null);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            int maxX = _image.getWidth() - 250;
            int maxY = _image.getHeight() - 190;
            _curX = _originalX + _downX - e.getX();
            _curY = _originalY + _downY - e.getY();
            if(_curX > maxX) _curX = maxX;
            if(_curX < 0) _curX = 0;
            if(_curY > maxY) _curY = maxY;
            if(_curY < 0) _curY = 0;
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) { }
        @Override
        public void mouseClicked(MouseEvent e) { }
        @Override
        public void mousePressed(MouseEvent e) {
            _downX = e.getX();
            _downY = e.getY();
            _originalX = _curX;
            _originalY = _curY;
        }
        @Override
        public void mouseReleased(MouseEvent e) { }
        @Override
        public void mouseEntered(MouseEvent e) { }
        @Override
        public void mouseExited(MouseEvent e) { }

        // variables used in mouse movement
        private int                 _curX = 0;
        private int                 _curY = 0;
        private int                 _downX = 0;
        private int                 _downY = 0;
        private int                 _originalX = 0;
        private int                 _originalY = 0;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _cancel;
    private javax.swing.JTextField _degree;
    private javax.swing.JLabel _degreeLabel;
    private javax.swing.JButton _ok;
    private javax.swing.JPanel _preview;
    private javax.swing.JLabel _previewLabel;
    // End of variables declaration//GEN-END:variables
    private BufferedImage   _srcImage = Implementor.Instance().getImage();
    private BufferedImage   _image = _srcImage;
}
